package com.zzj.hotel.basedao;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;



public class HibernateUtil {

	/**
	 * 
	 * TODO方法说明.获取查询字段
	 * 
	 * @param map 查询条件键值对
	 * @return
	 */
	public static void  getCriterion(DetachedCriteria criteria,Map<String, Object> map) {
		if(map != null){
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object value = map.get(key);
				if (value != null) {
					criteria.add(Property.forName(key).eq(map.get(key)));
				}
			}
		}
	}

	public static void getCriterion(String key,Object value,DetachedCriteria criteria, String match){


		if (value != null) {

			if(match.equals("is"))
				criteria.add(Property.forName(key).eq(value));
			else if(match.equals("gt"))
				criteria.add(Property.forName(key).gt(value));
			else if(match.equals("ge"))
				criteria.add(Property.forName(key).ge(value));
			else if(match.equals("lt"))
				criteria.add(Property.forName(key).lt(value));
			else if(match.equals("le"))
				criteria.add(Property.forName(key).le(value));
			else if(match.equals("in"))
				criteria.add(Property.forName(key).in((Collection) value));
		}	

	}

	public static void  getCriterion(Map<String, Object> map,DetachedCriteria criteria, String match) {

		if (map == null || map.size() == 0) {
			return ;
		}
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			Object value = map.get(key);
			getCriterion(key,value,criteria,match);
		}

	}


}
